﻿using Hangfire.Annotations;
using Hangfire.Dashboard;

namespace ApiApp;


/*
 * Hangfire Dashboard 的授权规则，在 app.MapHangfireDashboard 中使用
 */

/// <summary>
/// 异步版本
/// </summary>
public class AsyncHangfireAuthorizationFilter : IDashboardAsyncAuthorizationFilter
{
    public async Task<bool> AuthorizeAsync([NotNull] DashboardContext context)
    {
        var httpContext = context.GetHttpContext();

        await Task.Delay(TimeSpan.FromSeconds(1));

        // Allow all authenticated users to see the Dashboard (potentially dangerous).
        return httpContext.User.Identity?.IsAuthenticated ?? false;

    }
}

/// <summary>
/// 同步版本
/// </summary>
public class HangfireAuthorizationFilter : IDashboardAuthorizationFilter
{
    public bool Authorize([NotNull] DashboardContext context)
    {
        var httpContext = context.GetHttpContext();

        // Allow all authenticated users to see the Dashboard (potentially dangerous).
        return httpContext.User.Identity?.IsAuthenticated ?? false;
    }
}